#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "pack.h"
#include "file.h"
#include "users.h"
#include "tcp.h"

Unpack pack_info;
unsigned long seq;

void init_packinfo()				//初始化解包器
{
	pack_info.pack_num = (char *)malloc(12);
	pack_info.cmd_num = (char *)malloc(4);
}

void clean_packinfo()				//清空解包器
{
	memset(pack_info.pack_num, 0, 12);
	memset(pack_info.cmd_num, 0, 4);
	pack_info.addition = NULL;
}

void init_seq()						//初始化包编号
{
	srand(time(0));
	seq = rand();
}

void unpack_head(char *pack)		
{
	sscanf(pack, "%[^:]:%[^:]:", pack_info.pack_num
		, pack_info.cmd_num);
	pack_info.addition = pack + strlen(pack_info.pack_num) 
		+ strlen(pack_info.cmd_num) + 2;
}

int unpack_009(char *pack, int sockfd)
{
	int i;
	char offset[11], pktId[11];
	i = get_sFiles();						//busy状态 i为10，缓存pktId
	sscanf(pack, "%[^:]:%[^:]:%[^:]:", sFiles[i].filename,  offset, pktId);
	sFiles[i].offset = atol(offset);
	sFiles[i].pktId = atol(pktId);
	sFiles[i].sockfd = sockfd;
	return i;
}

int unpack_013(char *pack, int sockfd)
{
	int i;
	char pktId[11];
	i = get_rFiles();							//busy状态 i为10，缓存pktId
	sscanf(pack, "%[^:]:%[^:]:", rFiles[i].filename,  pktId);
	rFiles[i].pktId = atol(pktId);
	rFiles[i].sockfd = sockfd;
	return i;
}

void unpack_015(char *buf, int len, int sockfd)
{
	char pktId[11], size[4], *p;
	int index;
	sscanf(buf, "%[^:]:%[^:]", pktId, size);
	index = find_rFiles(sockfd, atol(pktId));
	if(rFiles[index].status != -1)				//接收文件
	{
		rFiles[index].status = 1;
		p = buf + (strlen(pktId) + strlen(size) + 2);
		write(rFiles[index].fileId, p, atoi(size));
	}
}

void unpack_016(char *buf, int sockfd)
{
	char filename[128], pktId[11];
	int index;

	sscanf(buf, "%[^:]:%[^:]:", filename, pktId);	
	index = find_rFiles(sockfd, atol(pktId));			//上传结束，清理接收序列
		rFiles[index].status = -1;
		close(rFiles[index].fileId);
		clean_rFiles(index);
}

int unpack_017(char *buf, int sockfd)				
{
	int info;
	char Account[21], Password[21];
	sscanf(buf, "%[^:]:%[^:]:", Account, Password);
	info = login(Account, Password);				//登陆验证
	if(info == 1)
		add_userdate(Account, sockfd);				//登陆成功，填入用户信息
	return info;
}

int unpack_022(char *buf, int sockfd)
{
	char Account[21], Password[21], Nickname[21];
	sscanf(buf, "%[^:]:%[^:]:%[^:]:", Account, Password, Nickname);

	return regist(Account, Password, Nickname);			//注册状况
}

void unpack_027(char *sendbuf, char *buf, int sockfd)
{
	Clnode *p;
	p = client_head;
	char data[1024], Account[21];
	sscanf(buf, "%[^:]:%[^:]:", Account, data);
	while(strcmp(p->user_data.Account, Account) != 0)	//目标
		p = p -> next;
	if(p != NULL)
		pack_028(sendbuf, data, p -> sockfd, sockfd);
}

void unpack_029(char *buf)
{
	char Account[21], Nickname[21], Motto[51], Email[51], Pic[2];
	sscanf(buf, "%[^:]:%[^:]:%[^:]:%[^:]:%[^:]:", Account, Nickname, Motto, Email, Pic);
	change(Account, Nickname, Motto, Email, atoi(Pic));
}

void pack_008(char *pack)
{
	char data[1024];
	memset(pack, 0, 1024);
	seq++;
	list_file(data);									//装填服务器文件列表
	sprintf(pack, "%ld:%s:%s", seq, "008", data);
}

void pack_010(char *pack, int i)
{
	int busy = 0;
	memset(pack, 0, 1024);
	seq++;
	if(i == 10)
		busy = 1;
	sprintf(pack, "%ld:%s:%s:%ld:%ld:%d:", seq , "010", 
		sFiles[i].filename, sFiles[i].offset, sFiles[i].pktId, busy);
	if(i == 10)
		clean_sFiles(i);								//忙碌状态  清理缓存
}

int pack_011(char *pack, char *filebuf, int i, int flen)
{
	int len;
	memset(pack, 0, 1024);
	seq++;
	len = sprintf(pack, "%ld:%s:%ld:%d:%s", seq, "011", sFiles[i].pktId, flen, filebuf);
	return len;
}

void pack_012(char *pack, int i)
{
	memset(pack, 0, 1024);
	seq++;
	sprintf(pack, "%ld:%s:%ld:", seq, "012", sFiles[i].pktId);
}

void pack_014(char *pack, int i)
{
	int busy = 0;
	memset(pack, 0, 1024);
	seq++;
	if(i == 10)
		busy = 1;
	sprintf(pack, "%ld:%s:%s:%ld:%d:", seq, "014", rFiles[i].filename, rFiles[i].pktId, busy);
	if(i == 10)											//接收忙碌，清空缓存
	{
		close(rFiles[i].fileId);
		clean_rFiles(i);
	}
}

void pack_018(char *pack, int info)
{
	memset(pack, 0, 1024);
	seq++;
	sprintf(pack, "%ld:%s:%d:", seq, "018", info);
}

void pack_020(char *pack, char *buf, int num)
{
	memset(pack, 0, 1024);
	seq++;
	sprintf(pack, "%ld:%s:%d:%s", seq, "020", num, buf);
}

void pack_021(char *pack, int sockfd)
{
	Clnode *p;
	memset(pack, 0, 1024);
	seq++;
	p = find_client(sockfd);			
	sprintf(pack, "%s",p->user_data.Account);
}

void pack_023(char *pack, int busy)
{
	memset(pack, 0, 1024);
	seq++;
	sprintf(pack, "%ld:%s:%d:", seq, "023", busy);
}

void pack_024(char *pack, int sockfd)
{
	Clnode *p;
	memset(pack, 0, 1024);
	seq++;
	p = find_client(sockfd);
	sprintf(pack, "%s:%s:%s:%s:%d",p->user_data.Account,   
					p->user_data.Nickname, p->user_data.Motto, 
					p->user_data.Email, p->user_data.Pic);
}

void pack_025_data(char *pack, char *data, int sockfd)
{
	Clnode *p;
	memset(pack, 0, 1024);
	seq++;
	p = find_client(sockfd);
	sprintf(pack, "%s:%s:%s", p->user_data.Nickname, p->user_data.Account, data);

}

void pack_028(char *pack, char *data, int sockfd, int srcsockfd)
{
	Clnode *p;
	memset(pack, 0, 1024);
	seq++;
	p = find_client(srcsockfd);			//源地址socket
	sprintf(pack, "%ld:%s:%s:%s", seq, "028", p->user_data.Account, data);
	sock_send(pack, sockfd);
}